﻿/* 功能:  产品管理
 * 创建人：Kencery  创建时间：2018-2-3
 */
var ml = window.ml || {};
ml.admin = ml.admin || {};

ml.admin.product = (function () {
    var defaults = {
        listUrl: "/Product/List",
        delUrl: "/Product/Delete",
        detailUrl: "/Product/GetDetailById"
    };
    var opt = {};

    var columns = [{
        field: 'state',
        radio: true
    }, {
        title: '行号',
        field: 'number',
        align: 'center',
        hide: true,
        formatter: plscommon.tableNumber
    }, {
        title: '产品ID',
        field: 'Id',
    }, {
        title: '产品分类',
        field: 'ProductCategoryName',
        align: 'center',
    }, {
        title: '商户名',
        field: 'MerchantName',
        align: 'center',
    }, {
        title: '名称',
        field: 'Name',
        align: 'center',
    }, {
        title: '预览图',
        field: 'PreviewImg',
        align: 'center',
        formatter: function (value, rows, index) {
            return '<div><img src="' + value + '" style="width:40px;height:40px;"></div>';
        }
    }, {
        title: '原价',
        field: 'OriginalPrice',
        align: 'center',
    }, {
        title: '折扣价',
        field: 'DiscountPrice',
        align: 'center',
    }, {
        title: '销量',
        field: 'SalesVolume',
        align: 'center',
    }, {
        title: '好评率',
        field: 'PraiseRate',
        align: 'center',
    }, {
        title: '大小中份',
        field: 'Type',
        align: 'center',
    }, {
        title: '限购',
        field: 'IsLimitPurchase',
        align: 'center',
        formatter: function (value, rows, index) {
            var result = returnIsLimitPurchase(value);
            return '<div id="State_' + rows.Id + '">' + result + '</div>';
        }
    }, {
        title: '限购份数',
        field: 'LimitPurchaseCopies',
        align: 'center',
    }, {
        title: '上下架',
        field: 'UALMark',
        align: 'center',
        formatter: function (value, rows, index) {
            var result = returnUALMark(value);
            return '<div id="State_' + rows.Id + '">' + result + '</div>';
        }
    }, {
        title: '创建时间',
        field: 'CreateTime',
        align: 'center',
    }];

    var initTable = function () {
        plscommon.bootstraptable({
            id: "#productinfo",
            url: defaults.listUrl,
            queryParams: queryParams,
            uniqueId: "Id",
            columns: columns
        });
    }

    var queryParams = function (params) {
        return {
            offset: params.offset,             //后台计算显示数据信息
            pagesize: params.limit,            //每页显示多少行
            merchantname_search: $("#merchantname_search").val(),
            name_search: $("#name_search").val(),
            ualmark_search: $("#ualmark_search").val()
        };
    }

    var clickEvent = function () {
        $("#btnQueryList").on("click", function () { btnQueryList(); });                      //按条件查询结果
        $("#btnReset").on("click", function () { btnReset(); });                              //清空文本框信息

        $("#btnDelDialog").on("click", function () { btnDelDialog(); });                      //产品删除弹层
        $("#btnDeleteOK").on("click", function () { btnDeleteOK(); });                        //产品删除

        $("#btnDetail").on("click", function () { btnDetail(); });                            //订单详情
    }

    var btnQueryList = function () {
        plscommon.refreshTable("productinfo");
    }

    var btnReset = function () {
        plscommon.resetFrom("formSearch");
    }

    var btnDelDialog = function () {
        var data = plscommon.dialogWaring('productinfo', 'DeleteModal', 'DeleteOperation');
        if (!data.Id) {
            return false;
        }

        $("#delete_id").val(data.Id);
        $("#Delete_Head_Title").text(data.Name);
        $("#Delete_Head_Title_OK").text(data.Name);
    }

    var btnDeleteOK = function () {
        var delete_id = $("#delete_id").val();
        if (!delete_id) {
            plscommon.warningMessage("请您选择需要删除的数据", 4000);
            return false;
        }

        $("#btnDeleteOK").prop("disabled", true);
        plscommon.ajax({
            disableId: "btnDeleteOK",
            url: defaults.delUrl,
            type: "POST",
            data: { id: delete_id },
            success: function () {
                $('#DeleteModal').modal('hide');
                $("#productinfo").bootstrapTable('refresh');
            }
        });
    }

    var btnDetail = function () {
        //必须选中一条才能弹出
        var data = plscommon.dialogWaring('productinfo', 'DetailModal', 'ProductDetail');
        if (!data.Id) {
            return false;
        }

        plscommon.ajax({
            url: defaults.detailUrl,
            type: "GET",
            data: { id: data.Id },
            success: function () {
                var data = this.data;
                $("#detail_platformname").val(data.ProductCategoryName);
                $("#detail_merchantname").val(data.MerchantName);
                $("#detail_name").val(data.Name);
                $("#detail_salesvolume").val(data.SalesVolume);
                $("#detail_originalprice").val(data.OriginalPrice);
                $("#detail_discountprice").val(data.DiscountPrice);
                $("#detail_description").val(data.Description);
                $("#detail_tag").val(data.Tag);
                $("#detail_praiserate").val(data.PraiseRate);

                $("#detail_type").val(data.Type);
                $("#detail_islimitpurchase").val(returnIsLimitPurchase(data.IsLimitPurchase));
                $("#detail_limitpurchasecopies").val(data.LimitPurchaseCopies);
                $("#detail_ualmark").val(returnUALMark(data.UALMark));
                $("#detail_createtime").val(data.CreateTime);
                $("#detail_previewimg").attr("src", data.PreviewImg);
            }
        });
    }

    var returnIsLimitPurchase = function (value) {
        var result = "";
        if (value == 0) {
            result = "不限购";
        }
        if (value == 1) {
            result = "限购";
        }
        return result;
    }

    var returnUALMark = function (value) {
        var result = "";
        if (value == 0) {
            result = "未上架";
        }
        if (value == 1) {
            result = "已上架";
        }
        return result;
    }

    return {
        init: function (options) {
            $.extend(defaults, options || {});
            initTable();            //初始化Table表格
            clickEvent();           //触发事件
        }
    };
}());